﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>

<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

 <?php 
  if (($_FILES['upload'])){
	$message .= "</br> files Upload is set";
	if (($_FILES['upload']['error']) == 0){
		$message .= "</br> files Upload no errors";
		$file_name = ($_FILES['upload']['name']);
		$file_type = ($_FILES['upload']['type']);		
		$file_size = ($_FILES['upload']['size']);		
		$file_tmp_path = ($_FILES['upload']['tmp_name']);
		$message .= "</br>". $file_name ." ". $file_type ." ". $file_size ." ". $file_tmp_path."</br>";

  //================================================================================//
 //            Dabar turetu buti patikrinimai del tipo, bet tingiu daryt :P		   //
//================================================================================//
		
		$target_file = "images/". time() . ".jpg";
		$message .= $target_file;
		if(move_uploaded_file($file_tmp_path, $target_file)) {
			$message .= "</br> File moved Successfullllyyyyy!!!!";
			//and we should make a record to database......
			//I'm so LAZY....
			//Can I skip this part?
			$main = 1;
			if (($_POST['main']) != 0){
				$qw = 0;
				$sql = "UPDATE `foto` SET";
				$sql .= " `pagrindine` =";
				$sql .= $qw;
				// $sql .= " `pavarde` =";
				// $sql .= "'". $pavarde . "', ";
				// $sql .= " `tevavardis` =";
				// $sql .= "'". $tevavardis . "', ";
				// $sql .= " `gimimodata` =";
				// $sql .= "'". $gimimodata . "', ";
				// $sql .= " `ak` =";
				// $sql .= "'". $ak . "', ";
				// $sql .= " `bylosnr` =";
				// $sql .= "'". $bylosnr . "' ";
				$sql .= " WHERE `fotoid` = ";
				$sql .= $foto_id;
				$sql .= " LIMIT 1;";
				
				$message .= "</br> ||||| " . $sql . " |||||| </br>";
				
		$result = mysql_query($sql, $connection);
			if ($result) {
				$message .= "</br>Client's old main photo depricated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client's old main photo's Deprication failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
			};
			$message .= "</br> --> ".$main." <-- </br>";
			
			
			$sql = "INSERT INTO `foto` (`userid`, `path`, `pagrindine`) ";
			$sql .= "VALUES (";
			$sql .= "'". ($_GET['userid']) . "', ";
			$sql .= "'". $target_file . "', ";
			$sql .= $main;
			//$sql .= "'". $gimimodata . "', ";
			//$sql .= "'". $ak . "', ";
			//$sql .= "'". $bylosnr . "' ";
			$sql .= ");";
			//echo "SQL naujam: ".$sql; //viskas OK
		
		$result = mysql_query($sql, $connection);
			if ($result) {
				// Success!
				$temp = mysql_insert_id();
				$foto_location = $target_file;
				$message .= "</br>Photo record inserted Successsullyy!! with ID:".$temp." </br>";
			} else {
				// Display error message.
				$message .= "<p>Subject creation failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
			
		};	
		
	} else {
		$message .= "</br> files Upload got error:";
		$message .= ($_FILES['upload']['error']);
	};
  } else { 
	$message = "</br>  is NOT set";};
//};

 ?>

 
					<article>
						<h2>Nuotraukos ikelimas</h2>
	<?php echo $message."</br>";?>
	<?php //pagrindiniai kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);
	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form action="photo_upload.php?userid=<?php echo ($_GET['userid']);?>" method="post" enctype="multipart/form-data" name="upload">
  
  </br>
  
  Pasirikite faila: <input name="upload" type="file" size="15" /> 
<?php //dadedam checkbox'a jeigu yra pagrindine
	if ($pagridine_yra == 1){ 
		echo "</br> Daryti pagrindine <input name=\"main\" type=\"checkbox\" value=\"1\" checked>";
	};
?>

</br>
<input name="Submit" type="submit" value="SubmiNt" />
</form>


			</article>
            
<?php require("includes/footer.php"); ?>
